import React from 'react';
import {
    Button, Input, Form, message, InputNumber
} from 'antd';
import * as Request from '@/services/Request';
import Checkbox from 'antd/lib/checkbox/Checkbox';

const apiURL = "/api/services/app/WhseBin/";
class WhseBinEntry extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            dataSource: [],
        }
    }
    formRef: any = React.createRef();
    componentDidMount() {
        this.fetchDatas();
    }
    onFinish = (values: any) => {
        var model = values.model;
        model.id = this.props.id;
        this.setState({ loading: true });
        var act = "Modify";
        if (model.id == 0 || !model.id) {
            act = "New";
        }
        model.zoneID = "0";
        model.binType = "0";
        Request.post(apiURL + act, model).then((response) => {
            if (response) {
                if (response.success) {
                    if (response.result.success) {
                        message.success('保存成功！');
                        this.props.fetchData();
                    }
                    else {
                        message.error(response.result.message);
                    }
                }
                else {
                    message.error(response.error.message);
                }
                this.setState({ loading: false });
            }
        });
    }
    fetchDatas = () => {
        const { id, WareHse } = this.props;
        var model: any = {}
        model.warehouseCode = WareHse?.warehouseCode;
        if (id > 0) {
            Request.get(apiURL + 'Get?Id=' + id).then((response) => {
                model = response?.result;
                this.formRef.current.setFieldsValue({ model });
            });
        }
        else
            this.formRef.current.setFieldsValue({ model });

    }
    render() {
        const layout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 19 },
        };
        return (
            <Form {...layout} ref={this.formRef} onFinish={this.onFinish} >
                <Form.Item label="仓库代码" style={{marginBottom:0}}>
                    <Input.Group compact>
                        <Form.Item name={['model', 'warehouseCode']} rules={[{ required: true }]}>
                            <Input disabled />
                        </Form.Item>
                        <Form.Item>
                            <Input disabled value={this.props.WareHse?.description} />
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Form.Item name={['model', 'binNum']} label="库位代码" rules={[{ required: true }]}>
                    <Input />
                </Form.Item>
                <Form.Item name={['model', 'description']} label="库位描述" rules={[{ required: true }]}>
                    <Input />
                </Form.Item>
                <Form.Item name={['model', 'maxFill']} label="最大库存">
                    <InputNumber min={0} />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 5 }}>
                    <Input.Group compact>
                        <Form.Item name={['model', 'nonNettable']} valuePropName="checked">
                            <Checkbox>不计入库存</Checkbox>
                        </Form.Item>
                        <Form.Item name={['model', 'inActive']} valuePropName="checked">
                            <Checkbox>无效</Checkbox>
                        </Form.Item>
                    </Input.Group></Form.Item>
                <Form.Item wrapperCol={{ offset: 5 }}>
                    <Button type="primary" htmlType="submit">保存</Button>
                </Form.Item>
            </Form>
        )
    }
}
export default WhseBinEntry